import { getListApi, getPageStaticApi, deliveryApi, downloadOutWarehouseDocApi } from "@/api/waybillManagement/outWarehouseReceipt";
import CommonHook from "@/hook/commonHook";
import { tableColumnMap } from "../data";
import { message, Modal } from "ant-design-vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { computed, createVNode } from "vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { searchForm, tableData, drawerOpts, loading, activeKey, pageStat, tableRef, dialogOpts } = state;
  const selectRows = computed<Array<OutWarehouseReceiptManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { getNoZeroKey, downloadFileByBlob } = CommonHook();
  const getData = async () => {
    const params = {
      ...getParams(),
      status: Number(activeKey.value)
    };
    loading.value = true;
    tableData.value = [];
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi(params).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, data);
        activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2", "3"]));
      }
    });
  };
  const getParams = () => {
    const { outWarehouseNumList, waybillNumList, outWarehouseIdList, pageNum, pageSize, companyIdList, total } = searchForm;
    const params: Parameters<typeof getListApi>[0] = {
      outWarehouseNumList: [],
      waybillNumList: [],
      pageNum,
      pageSize,
      outWarehouseIdList,
      companyIdList,
      total
    };
    if (waybillNumList && waybillNumList.length) {
      params.waybillNumList = waybillNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    }

    if (outWarehouseNumList && outWarehouseNumList.length)
      params.outWarehouseNumList = outWarehouseNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  const editHandle = (row: OutWarehouseReceiptManagement.ListRows) => {
    drawerOpts.title = `编辑(${row.preOrderNo})`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 2;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  const detailsHandle = (row: OutWarehouseReceiptManagement.ListRows) => {
    drawerOpts.title = `出仓单明细`;
    drawerOpts.width = "1200px";
    drawerOpts.component = "Details";
    drawerOpts.type = 1; //详情
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  const driverHandle = (row: OutWarehouseReceiptManagement.ListRows) => {
    drawerOpts.title = `配置司机`;
    drawerOpts.width = "1200px";
    drawerOpts.component = "Details";
    drawerOpts.type = 2; //配置司机
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  const deliveryHandle = () => {
    if (!selectRows.value.length) return message.error("请勾选数据!");
    const idList = selectRows.value.map((t: any) => t.id);

    Modal.confirm({
      title: "是否交货?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        deliveryApi({ idList }).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功");
            searchHandle();
          }
        });
      }
    });
  };
  //下载出仓单
  const downLoadOutWarehouseReceipt = (row: Base.GeneraDataType) => {
    downloadOutWarehouseDocApi({ id: row.id }).then((res: any) => {
      downloadFileByBlob(res, "出仓单.xlsx");
    });
  };

  const setDriverHandle = () => {
    if (!selectRows.value.length) return message.error("请勾选数据!");
    const agemt = Array.from(new Set(selectRows.value.map((t: any) => t.brokerName)));
    if (agemt.length > 1) return message.error("只能选择同一代理进行配置");
    dialogOpts.title = `配置出仓司机`;
    dialogOpts.width = "500px";
    dialogOpts.component = "SetDirver";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  const tableColumn = computed<Array<SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem>>(() => {
    return tableColumnMap[activeKey.value];
  });
  return {
    searchHandle,
    getData,
    editHandle,
    changeTabHandle,
    pageStateic,
    tableColumn,
    deliveryHandle,
    detailsHandle,
    driverHandle,
    downLoadOutWarehouseReceipt,
    setDriverHandle
  };
}
